package test.util;


import zhengqc.sort.Sort;

public class SortHelper {
    private SortHelper() {}
    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; ++i) {
            if (arr[i-1].compareTo(arr[i]) > 0) {
                return false;
            }
        }
        return true;
    }
    public static <E extends Comparable<E>> void sortTest(Sort sortObject, E[] arr) {
        long startTime = System.nanoTime();
        sortObject.sort(arr);
        long endTime = System.nanoTime();

        if (! SortHelper.isSorted(arr)) {
            throw new RuntimeException(sortObject.sortName + " failed!");
        }
        double time = (endTime - startTime) / 1e9;
        System.out.printf("%s,\tn = %d,\ttime: %fs\n", sortObject.sortName, arr.length, time);
    }
}
